<!--  -->
<template>
  <div class="meedu-main-body">
    <div class="float-left j-b-flex mb-30">
      <div class="d-flex">
        <el-button @click="deletePromoCode()" type="danger"
          >批量删除
        </el-button>
        <el-button @click="$router.push({ name: 'Createcode' })" type="primary"
          >添加
        </el-button>
        <el-button @click="$router.push({ name: 'CodeImport' })" type="primary"
          >批量导入
        </el-button>
        <el-button
          @click="$router.push({ name: 'CreateMulticode' })"
          type="primary"
          >批量生成
        </el-button>
      </div>

      <div class="d-flex">
        <div>
          <el-input
            class="w-150px"
            v-model="filter.key"
            placeholder="优惠码"
          ></el-input>
        </div>
        <div class="ml-10">
          <el-input
            class="w-150px"
            v-model="filter.user_id"
            placeholder="学员ID"
          ></el-input>
        </div>
        <div class="ml-10">
          <el-button @click="paginationReset()">清空</el-button>
          <el-button @click="firstPageLoad()" type="primary"> 筛选 </el-button>
        </div>
        <div class="drawerMore d-flex ml-10" @click="drawer = true">
          <template v-if="showStatus">
            <img src="@/assets/img/icon-filter-h.png" />
            <span class="act">已选</span>
          </template>
          <template v-else>
            <img src="@/assets/img/icon-filter.png" />
            <span>更多</span>
          </template>
        </div>
      </div>
    </div>
    <div class="float-left">
      <div class="float-left">
        <el-table
          :header-cell-style="{ background: '#f1f2f9' }"
          :data="list"
          @selection-change="handleSelectionChange"
          class="float-left"
        >
          <el-table-column type="selection" width="55"></el-table-column>
          <el-table-column prop="id" label="ID" width="120"> </el-table-column>
          <el-table-column
            prop="invited_user_reward"
            width="300"
            label="优惠码"
          >
            <template slot-scope="scope">
              <div class="mb-10">{{ scope.row.code }}</div>
              <div>面值：{{ scope.row.invited_user_reward }}元</div>
              <div>奖励：{{ scope.row.invite_user_reward }}元</div>
            </template>
          </el-table-column>

          <el-table-column label="可使用次数" width="300">
            <template slot-scope="scope">
              <el-tag type="danger" v-if="scope.row.use_times === 0">
                不限制
              </el-tag>
              <el-tag type="info" v-else>
                {{ scope.row.use_times || 0 }}次
              </el-tag>
            </template>
          </el-table-column>

          <el-table-column label="已使用次数" width="150">
            <template slot-scope="scope">
              {{ scope.row.used_times || 0 }}次
            </template>
          </el-table-column>

          <el-table-column label="过期时间" width="200">
            <template slot-scope="scope">{{
              scope.row.expired_at | dateFormat
            }}</template>
          </el-table-column>

          <el-table-column label="添加时间">
            <template slot-scope="scope">{{
              scope.row.created_at | dateFormat
            }}</template>
          </el-table-column>
        </el-table>
      </div>

      <div class="float-left mt-30 text-center">
        <el-pagination
          @size-change="paginationSizeChange"
          @current-change="paginationPageChange"
          :current-page="pagination.page"
          :page-sizes="[10, 20, 50, 100]"
          :page-size="pagination.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        >
        </el-pagination>
      </div>
    </div>
    <el-drawer :size="360" :visible.sync="drawer" :with-header="false">
      <div class="n-padding-box">
        <div class="tit flex">更多筛选</div>
        <div class="j-flex">
          <el-input
            class="w-300px"
            v-model="filter.key"
            placeholder="优惠码"
          ></el-input>
        </div>
        <div class="j-flex mt-20">
          <el-input
            class="w-300px"
            v-model="filter.user_id"
            placeholder="学员ID"
          ></el-input>
        </div>

        <div class="j-flex mt-20">
          <el-date-picker
            v-model="filter.expired_at"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="过期时间-开始"
            end-placeholder="过期时间-结束"
          >
          </el-date-picker>
        </div>

        <div class="j-flex mt-20">
          <el-date-picker
            v-model="filter.created_at"
            type="daterange"
            align="right"
            unlink-panels
            range-separator="至"
            start-placeholder="添加时间-开始"
            end-placeholder="添加时间-结束"
          >
          </el-date-picker>
        </div>
        <div class="j-b-flex mt-30">
          <el-button @click="paginationReset()">清空</el-button>
          <el-button @click="firstPageLoad()" type="primary"> 筛选 </el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>

<script>
//这里可以导入其他文件（比如：组件，工具js，第三方插件js，json文件，图片文件等等）
//例如：import 《组件名称》 from '《组件路径》';

export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    //这里存放数据
    return {
      pageName: "promocode-list",
      pagination: {
        video_id: this.$route.query.id,
        course_id: this.$route.query.course_id,
        page: 1,
        size: 10,
      },
      filter: {
        user_id: null,
        key: null,
        created_at: null,
        expired_at: null,
      },
      spids: {
        ids: [],
      },
      total: 0,
      loading: false,
      list: [],
      drawer: false,
    };
  },
  //监听属性 类似于data概念
  computed: {
    showStatus() {
      if (
        this.filter.user_id ||
        this.filter.key ||
        this.filter.created_at ||
        this.filter.expired_at
      ) {
        return true;
      }
      return false;
    },
  },
  //监控data中的数据变化
  watch: {
    "$route.query.id"() {
      this.pagination.page = 1;
      this.filter.user_id = null;
      this.filter.key = null;
      this.filter.created_at = null;
      this.filter.expired_at = null;
      this.spids.ids = [];
    },
  },
  //方法集合
  methods: {
    firstPageLoad() {
      this.pagination.page = 1;
      this.getData();
      this.drawer = false;
    },
    paginationReset() {
      this.pagination.page = 1;
      this.filter.user_id = null;
      this.filter.key = null;
      this.filter.created_at = null;
      this.filter.expired_at = null;
      this.getData();
      this.drawer = false;
    },
    paginationSizeChange(size) {
      this.pagination.size = size;
      this.getData();
    },
    paginationPageChange(page) {
      this.pagination.page = page;
      this.getData();
    },
    //保存选中结果
    handleSelectionChange(val) {
      var newbox = [];
      for (var i = 0; i < val.length; i++) {
        newbox.push(val[i].id);
      }
      this.spids.ids = newbox;
    },
    getData() {
      let params = {};
      this.pagination.video_id = this.$route.query.id;
      this.pagination.course_id = this.$route.query.course_id;
      Object.assign(params, this.filter, this.pagination);
      this.$request.get(`promoCode`, { params: params }).then((res) => {
        this.list = res.data.data;
        this.total = res.data.total;
      });
    },
    deletePromoCode() {
      if (this.spids.ids == "") {
        this.$message.error("请选择需要操作的数据");
        return;
      }
      this.$confirm("确认操作？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //点击确定按钮的操作
          this.$request
            .post(`promoCode/delete/multi`, this.spids)
            .then(() => {
              this.$message.success(this.$t("common.success"));
              this.getData();
            })
            .catch((e) => {
              this.$message.error(e.message);
            });
        })
        .catch((e) => {
          // 点×不提示
        });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.getData();
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {},
  beforeCreate() {}, //生命周期 - 创建之前
  beforeMount() {}, //生命周期 - 挂载之前
  beforeUpdate() {}, //生命周期 - 更新之前
  updated() {}, //生命周期 - 更新之后
  beforeDestroy() {}, //生命周期 - 销毁之前
  destroyed() {}, //生命周期 - 销毁完成
  activated() {}, //如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='less' scoped>
:deep(.el-date-editor .el-range-separator) {
  width: auto;
}
.w-150px {
  width: 150px;
}
</style>